package com.example.serverdemo.utils.fileutils;

import com.example.serverdemo.utils.log.PrintLogUtil;

import java.io.*;

public class FirmwareFileUtils {
    private static FirmwareFileUtils instance;
    private File sourceFile;
    private FirmwareFileUtils() {
    }
    public static FirmwareFileUtils getInstance(){
        if (instance == null) instance = new FirmwareFileUtils();
        return instance;
    }
    private boolean duplicateFirmwareFile(String deviceId,String sourceFilePath){
       try {
           sourceFile = new File(sourceFilePath);
           synchronized (sourceFile){
               String fileName = "C://Server/CopyFile/" + deviceId + ".xbin";
//               String fileName = "/Users/xiaosa/Documents/CopyFile/" + deviceId + ".xbin";
               File file = new File(fileName);
               if (file.exists()){
                   file.delete();
               }
               file.createNewFile();
               FileInputStream inputStream = new FileInputStream(sourceFile);
               FileOutputStream outputStream = new FileOutputStream(file);
               byte[] buff = new byte[1024 * 8];
               int len = 0;
               while ((len = inputStream.read(buff)) != -1){
                   outputStream.write(buff,0,len);
               }
               inputStream.close();
               outputStream.close();
           }
           return true;
       }catch (Exception e){
           PrintLogUtil.printError(this,e);
       }
       return false;
    }
}
